<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="运单号" prop="shippingNoteNumber">
        <el-input
          v-model="queryParams.shippingNoteNumber"
          placeholder="请输入运单号"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="手机号" prop="driverPhone">
        <el-input
          v-model="queryParams.driverPhone"
          placeholder="请输入司机手机号"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <!--<el-row :gutter="10" class="mb8">-->
      <!--<el-col :span="1.5">-->
        <!--<el-button-->
          <!--type="primary"-->
          <!--plain-->
          <!--icon="el-icon-plus"-->
          <!--size="mini"-->
          <!--@click="handleAdd"-->
          <!--v-hasPermi="['tws:monetaryChange:add']"-->
        <!--&gt;新增</el-button>-->
      <!--</el-col>-->
      <!--<el-col :span="1.5">-->
        <!--<el-button-->
          <!--type="success"-->
          <!--plain-->
          <!--icon="el-icon-edit"-->
          <!--size="mini"-->
          <!--:disabled="single"-->
          <!--@click="handleUpdate"-->
          <!--v-hasPermi="['tws:monetaryChange:edit']"-->
        <!--&gt;修改</el-button>-->
      <!--</el-col>-->
      <!--<el-col :span="1.5">-->
        <!--<el-button-->
          <!--type="danger"-->
          <!--plain-->
          <!--icon="el-icon-delete"-->
          <!--size="mini"-->
          <!--:disabled="multiple"-->
          <!--@click="handleDelete"-->
          <!--v-hasPermi="['tws:monetaryChange:remove']"-->
        <!--&gt;删除</el-button>-->
      <!--</el-col>-->
      <!--<el-col :span="1.5">-->
        <!--<el-button-->
          <!--type="warning"-->
          <!--plain-->
          <!--icon="el-icon-download"-->
          <!--size="mini"-->
          <!--@click="handleExport"-->
          <!--v-hasPermi="['tws:monetaryChange:export']"-->
        <!--&gt;导出</el-button>-->
      <!--</el-col>-->
      <!--<right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>-->
    <!--</el-row>-->

    <el-table v-loading="loading" :data="monetaryChangeList" @selection-change="handleSelectionChange" border>
      <el-table-column type="selection" width="55" align="center" />
      <!--<el-table-column label="ID" align="center" prop="id" />-->
      <!--<el-table-column label="订单ID" align="center" prop="orderId" />-->
      <el-table-column label="运单号" align="center" prop="shippingNoteNumber" />
      <!--<el-table-column label="司机id" align="center" prop="driverId" />-->
      <el-table-column label="司机名" align="center" prop="driverName" />
      <el-table-column label="司机手机号" align="center" prop="driverPhone" />
      <el-table-column label="期望运费" align="center" prop="desireMonetaryAmount" />
      <el-table-column label="运费金额" align="center" prop="totalMonetaryAmount" />
      <el-table-column label="状态" align="center" prop="status" :formatter="statusFormat"/>
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            @click="handleView(scope.row)"
            v-hasPermi="['tws:secure:edit']"
          >查看订单</el-button>
          <el-button
            size="mini"
            type="text"
            style="color: limegreen"
            @click="change(scope.row)"
            v-hasPermi="['tws:monetaryChange:edit']"
            v-if="scope.row.status == 0"
          >通过</el-button>
          <el-button
            size="mini"
            type="text"
            style="color: red"
            @click="reject(scope.row)"
            v-hasPermi="['tws:monetaryChange:remove']"
            v-if="scope.row.status == 0"
          >驳回</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.current"
      :limit.sync="queryParams.size"
      @pagination="getList"
    />

    <!-- 添加或修改运费变更申请对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="订单ID" prop="orderId">
          <el-input v-model="form.orderId" placeholder="请输入订单ID" />
        </el-form-item>
        <el-form-item label="运单号" prop="shippingNoteNumber">
          <el-input v-model="form.shippingNoteNumber" placeholder="请输入运单号" />
        </el-form-item>
        <el-form-item label="司机id" prop="driverId">
          <el-input v-model="form.driverId" placeholder="请输入司机id" />
        </el-form-item>
        <el-form-item label="司机名" prop="driverName">
          <el-input v-model="form.driverName" placeholder="请输入司机名" />
        </el-form-item>
        <el-form-item label="司机手机号" prop="driverPhone">
          <el-input v-model="form.driverPhone" placeholder="请输入司机手机号" />
        </el-form-item>
        <el-form-item label="期望运费" prop="desireMonetaryAmount">
          <el-input v-model="form.desireMonetaryAmount" placeholder="请输入期望运费" />
        </el-form-item>
        <el-form-item label="运费金额" prop="totalMonetaryAmount">
          <el-input v-model="form.totalMonetaryAmount" placeholder="请输入运费金额" />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" :disabled="disabled" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>

  </div>
</template>

<script>
import { listMonetaryChange, getMonetaryChange, delMonetaryChange, addMonetaryChange, updateMonetaryChange, rejectChange } from "@/api/tws/monetaryChange";

export default {
  name: "MonetaryChange",
  components: {
  },
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 禁用弹窗新增或修改的确定按钮
      disabled: false,
      rejectDisabled: false,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 运费变更申请表格数据
      monetaryChangeList: [],
      // 运单解除状态字典
      statusOptions: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      rejectOpen: false,
      // 查询参数
      queryParams: {
        current: 1,
        size: 10,
        orderId: null,
        shippingNoteNumber: null,
        driverId: null,
        driverName: null,
        driverPhone: null,
        desireMonetaryAmount: null,
        totalMonetaryAmount: null,
        status: null,
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        orderId: [
          { required: true, message: "订单ID不能为空", trigger: "blur" }
        ],
      }
    };
  },
  created() {
    this.getDict();
    this.getList();
  },
  methods: {
    //通过变更
    change(row){
      console.log("变更:",row);
      let that = this;
      let orderData ={};
      orderData.id = row.orderId;
      orderData.changeId = row.id;

      // orderData.driverLicense = row.driverLicense;
      // orderData.driverName = row.driverName;
      // orderData.driverPhone = row.telephone;
      this.$confirm('是否通过运单号为:['+row.shippingNoteNumber+']的运费变更申请',{
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning"
      }).then(function () {
        console.log(orderData)
        updateMonetaryChange(orderData).then(res =>{
          that.$modal.msgSuccess("申请成功!");
          that.getList();
        })
      })
    },
    // 驳回
    reject(row){
      let that = this;
      this.$confirm('是否驳回本单的运费变更申请',{
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning"
      }).then(function () {
        rejectChange(row).then(res =>{
          that.$modal.msgSuccess("已驳回!");
          that.getList();
        })
      })
    },
    // 查看
    handleView(row) {
      this.$router.push({ path: '/order/orderDetail', query: { orderId: row.orderId }, replace: true });
    },
    /** 查询字典 */
    getDict() {
      // 运单解除状态字典
      this.getDicts("order_secure_status").then(response => {
        this.statusOptions = response.data;
      })
    },
    /** 查询运费变更申请列表 */
    getList() {
      this.loading = true;
      listMonetaryChange(this.queryParams).then(response => {
        this.monetaryChangeList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        orderId: null,
        shippingNoteNumber: null,
        driverId: null,
        driverName: null,
        driverPhone: null,
        desireMonetaryAmount: null,
        totalMonetaryAmount: null,
        status: 0,
        createBy: null,
        createTime: null,
        updateBy: null,
        updateTime: null,
        delFlag: null,
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.current = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加运费变更申请";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const id = row.id || this.ids
      getMonetaryChange(id).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改运费变更申请";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.disabled = true;
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.id != null) {
            updateMonetaryChange(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.disabled =false;
              this.open = false;
              this.getList();
            }).catch(err => {
              this.disabled =false;
            })
          } else {
            addMonetaryChange(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.disabled =false;
              this.open = false;
              this.getList();
            }).catch(err => {
              this.disabled =false;
            })
          }
        } else {
          this.disabled = false;
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$confirm('是否确认删除运费变更申请编号为"' + ids + '"的数据项?', "警告", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        }).then(function() {
          return delMonetaryChange(ids);
        }).then(() => {
          this.getList();
          this.$modal.msgSuccess("删除成功");
        }).catch(err => {
        })
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('tws/monetaryChange/export', {
        ...this.queryParams
      }, `tws_monetaryChange.xlsx`)
    },
    // 运单解除状态字典翻译
    statusFormat(row, column) {
      return this.selectDictLabel(this.statusOptions, row.status);
    },
  }
};
</script>
